package shoppingmall.reception.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.reception.dto.UserCartVO;
import shoppingmall.reception.mapper.CartMapper;
import shoppingmall.reception.mapper.CommercialSpecificationMapper;
import shoppingmall.reception.mapper.ProductMapper;
import shoppingmall.reception.pojo.CommercialSpecification;
import shoppingmall.reception.pojo.Product;
import shoppingmall.reception.pojo.UserCart;
import shoppingmall.reception.service.CartService;

import java.util.ArrayList;
import java.util.List;

/**
 * 平台会员用户购物车数据相关服务层
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CommercialSpecificationMapper commercialSpecificationMapper;  //商品规格mapper层实例
    @Autowired
    private ProductMapper productMapper;  //商品mapper层实例
    @Autowired
    private CartMapper cartMapper;  //购物车mapper层实例

    /**
     * 为某用户添加一条购物车数据
     */
    @Override
    public boolean saveUserCart(UserCart userCart) {
        //调用mapper层方法进行加入购物车操作
        //加购前进行判断，重复加购对其数量进行修改
        UserCart cartInfo = cartMapper.getCartInfoByCommodityIdAndStandardId(userCart.getCartCommodityId(), userCart.getCartCommodityStandardId());
        if(cartInfo == null){
            //购物车中无该商品，直接插入数据
            try {
                cartMapper.saveUserCart(userCart);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            //购物车表中有该数据，对其数量进行修改
            try {
                cartInfo.setCartCommodityCount(cartInfo.getCartCommodityCount() + 1);  //数量++
                cartMapper.updateUserCart(cartInfo);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        return true;
    }

    /**
     * 查询平台会员的所有购物车数据列表
     * @param userId
     * @return
     */
    @Override
    public List<UserCartVO> getUserCartList(int userId) {
        //调用mapper层方法

        //查询当前用户的所有购物车数据
        List<UserCart> userCartList = cartMapper.getUserCartList(userId);
        //给userCart上添加数据 cart_commodity_id --> 商品信息   cart_commodity_standard_id  --> 对应的规格信息
        List<UserCartVO> userCartVOS = new ArrayList<>();
        for (int i = 0; i < userCartList.size(); i ++){
            UserCartVO userCartVO = new UserCartVO();
            userCartVO.setId(userCartList.get(i).getId());
            userCartVO.setCartUserId(userCartList.get(i).getCartUserId());
            userCartVO.setCartCommodityId(userCartList.get(i).getCartCommodityId());
            userCartVO.setCartCommodityCount(userCartList.get(i).getCartCommodityCount());
            userCartVO.setCartCommodityAttr(userCartList.get(i).getCartCommodityAttr());
            userCartVO.setCartCommodityStandardId(userCartList.get(i).getCartCommodityStandardId());
            userCartVO.setCartSelected(userCartList.get(i).isCartSelected());
            userCartVO.setUpdateTime(userCartList.get(i).getUpdateTime());
            userCartVO.setCreateTime(userCartList.get(i).getCreateTime());
            //设置商品信息
            Product productInfo = productMapper.getProductInfoByProductId(userCartList.get(i).getCartCommodityId());
            userCartVO.setCommodityName(productInfo.getCommodityName());
            userCartVO.setCommodityImgs(productInfo.getCommodityImgs());
            //设置规格信息
            CommercialSpecification productAttrInfo = commercialSpecificationMapper.getCommercialSpecificationById(userCartList.get(i).getCartCommodityStandardId());
            userCartVO.setCommodityAttrName(productAttrInfo.getCommodityAttr());
            userCartVO.setCommodityAttrPrice((float) productAttrInfo.getCommodityPrice());
            //添加进集合中
            userCartVOS.add(userCartVO);
        }
        return userCartVOS;
    }

    /**
     * 删除平台会员的购物车数据(支持单条 和 多条数据批量删除)
     * @param cartIds
     * @return
     */
    @Override
    public boolean deleteUserCartBatch(List<Integer> cartIds) {
        //调用mapper层方法
        try {
            cartMapper.deleteUserCartBatch(cartIds);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 更新平台会员购物车的一条数据
     * @param userCart
     * @return
     */
    @Override
    public boolean updateUserCart(UserCart userCart) {
        //调用mapper层方法
        try {
            cartMapper.updateUserCart(userCart);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 修改平台会员用户购物车的选中状态(支持单条、多条批量修改)
     * @param cartIds 购物车主键id集合
     * @param state 是否选中，布尔值 true | false
     * @return
     */
    @Override
    public boolean updateUserCartSelectedBatch(List<Integer> cartIds, boolean state) {
        //调用mapper层方法
        try {
            cartMapper.updateUserCartSelectedBatch(cartIds, state);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
